package com.guangaiboen.attendance.core.dispatcher.executor;

import com.guangaiboen.attendance.core.dispatcher.constants.RequestTypes;
import com.guangaiboen.attendance.core.dispatcher.context.DispatcherContext;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.message.BasicNameValuePair;

import java.nio.charset.Charset;
import java.util.List;

public abstract  class FormEncodedHttpDispatcherExecutor<T > extends PostHttpDispatcherExecutor<T> {

    @Override
    protected StringEntity createEntity(String url, DispatcherContext dispatcherContext) {
        List<BasicNameValuePair> nameValuePair = getNameValuePair(dispatcherContext);
        UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(nameValuePair, getCharset());
        return urlEncodedFormEntity;
    }

    protected Charset getCharset(){
        return DEFAULT_CHARSET;
    }

    protected abstract List<BasicNameValuePair> getNameValuePair(DispatcherContext dispatcherContext);

    @Override
    protected String getRequestTypes() {
        return RequestTypes.FORMENCODED.getRequestCode();
    }

}
